package com.zyh.javaseshuzujinjiekaoshi;

public class Test3 {
    public static void main(String[] args) {
        int[] arr = {3, 12, 24, 36, 55, 68, 75, 88};
        int value = 24;
        int index = find(arr, value);
        System.out.println("index = " + index);
    }

    //    二分查找
    public static int find(int[] arr, int value) {
        int minIndex = 0, maxIndex = arr.length - 1, midIndex;
        while (minIndex <= maxIndex) {
            midIndex = (minIndex + maxIndex) / 2;
            for (int i = 0; i < arr.length; i++) {
                if (value > arr[midIndex]) {
                    minIndex = midIndex + 1;
                } else if (value < arr[midIndex]) {
                    maxIndex = midIndex - 1;
                } else {
                    return midIndex;
                }
            }
        }
        return -1;
    }
}
